<template>
  <div class="rollContainer">
    <div class="searchHead" ref="searchHead">
      <Form ref="formdata" :model="conditions" inline :label-width="120" :show-message="false">
        <div class="searchBox">
          <div class="conditionBox">
            <Row>
              <Col :span="6">
                <FormItem label="按展商名称查询">
                  <Select
                    v-model="conditions.directoryId"
                    clearable
                    @on-change="changeStatus($event, 'directoryId')"
                  >
                    <Option
                      v-for="item in directorys"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>
              <Col :span="6">
                <FormItem label="按展会查询">
                  <Select
                    v-model="conditions.miceId"
                    clearable
                    @on-change="changeStatus($event, 'miceId')"
                  >
                    <Option
                      v-for="item in mices"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>
             <Col :span="6">
                <FormItem label="按信息类型查询">
                  <Select
                    v-model="conditions.type"
                    clearable
                    @on-change="changeStatus($event, 'type')"
                  >
                    <Option
                      v-for="item in typeLists"
                      :value="item.value"
                      :key="item.value"
                    >{{ item.label }}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
          </div>
          <div class="searchBtnBox">
            <Button type="primary" @click="search()">搜索</Button>
            <Button @click="handleReset()">重置</Button>
          </div>
        </div>
      </Form>
      <!-- <div class="tableBtnBox">
        <Button icon="md-add" size="default" @click="Publish">新增合作伙伴</Button>
      </div>-->
    </div>
    <Table :columns="columns1" :height="tableHeight" border :data="list" :loading="loading">
      <template slot-scope="{ row }" slot="type">
        <span>{{row.type==0?'招商':'询盘'}}</span>
      </template>
      <template slot-scope="{ row }" slot="createTime">
        <span>{{row.create_time|dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
      </template>
      <template slot-scope="{ row }" slot="action">
        <span class="tableBtn" @click="show(row)">
          <Icon size="18" type="ios-create-outline" />编辑
        </span>
        <span class="tableBtn" @click="remove(row.id)">
          <Icon size="18" type="ios-trash" />删除
        </span>
      </template>
    </Table>
    <div class="pageBox">
      <Page
        :total="total"
        :page-size="pageSize"
        :current="page"
        size="small"
        show-total
        show-sizer
        @on-change="changePage"
        @on-page-size-change="changeSize"
      />
    </div>
    <Modal v-model="isShow" title="修改信息" width="1000">
      <Form ref="formdata" :model="formdata" :rules="rules" inline :label-width="100" fullscreen>
        <FormItem prop="title" label="标题">
          <Input type="text" v-model.trim="formdata.title" placeholder="请输入标题" style="width：300px"></Input>
        </FormItem>
        <FormItem prop="name" label="联系人姓名">
          <Input
            type="text"
            v-model.trim="formdata.name"
            placeholder="请输入联系人姓名"
            style="width：300px"
          ></Input>
        </FormItem>
        <FormItem prop="phone" label="联系人电话">
          <Input
            type="text"
            v-model.trim="formdata.phone"
            placeholder="请输入联系人电话"
            style="width：300px"
          ></Input>
        </FormItem>
        <FormItem prop="intro" label="项目简介">
          <Input
            type="textarea"
            v-model.trim="formdata.intro"
            :autosize="{minRows: 5,maxRows: 8}"
            placeholder="请输入项目简介"
          ></Input>
        </FormItem>
        <FormItem prop="pic" label="项目图片">
          <upload
            limit="100"
            @uploadImg="uploadImg($event,'pic',multiple=true)"
            @getImgUrl="getImgUrl($event,'pic',multiple=true)"
            :imgs="formdata.pic"
            :draggabled="true"
          ></upload>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="isShow=false">取消</Button>
        <Button type="primary" @click="submit('formdata')">确认</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import {
  merchantList,
  modifyMerchant,
  delMerchant,
  directoryList
} from "../../api/directory";
import { miceList } from "../../api/exhibitors";
import { _ValidatePhone } from "@/assets/js/validate.js";
import upload from "@/components/upload.vue";
export default {
  components: {
    upload
  },
  data() {
    const _ValidateImage = (rule, value, callback) => {
      if (value == "") {
        return callback(new Error("请上传项目图片"));
      } else {
        callback();
      }
    };
    return {
      limit: 1, //允许上传的数量
      typeLists: [
        {
          value: 0,
          label: "招商"
        },
        {
          value:1,
          label: "询盘"
        }
      ],
      list: [],
      page: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      conditions: {
        directoryId: "",
        miceId: "",
        type: ""
      },
      columns1: [
        {
          title: "id",
          key: "id",
          align: "center",
          minWidth: 200
        },
        {
          title: "标题",
          key: "title",
          align: "center",
          minWidth: 200,
          tooltip: true
        },
        {
          title: "项目简介",
          key: "intro",
          align: "center",
          minWidth: 200,
          tooltip: true
        },
        {
          title: "联系人姓名",
          key: "name",
          align: "center",
          minWidth: 200
        },
        {
          title: "联系人电话",
          key: "phone",
          align: "center",
          minWidth: 200
        },
        {
          title: "信息类型",
          slot: "type",
          align: "center",
          minWidth: 150
        },
        {
          title: "发布时间",
          slot: "createTime",
          align: "center",
          minWidth: 200
        },
        {
          title: "操作",
          slot: "action",
          width: 200,
          align: "center",
          fixed: "right"
        }
      ],
      tableHeight: 100,
      isShow: false,
      isFixed: false,
      username: "",
      id: "", //id
      directorys: [], //展商名录集合
      mices:[],
      formdata: {
        name: "",
        phone: "",
        title: "",
        intro: "",
        pic: []
      },
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur"
          }
        ],
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        intro: [
          {
            required: true,
            message: "请输入项目简介",
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            validator: _ValidatePhone,
            trigger: "blur"
          }
        ],
        pic: [
          {
            required: true,
            validator: _ValidateImage,
            trigger: "change"
          }
        ]
      },
      ruleInline: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        image: [
          {
            required: true,
            message: "请上传LOGO",
            trigger: "change"
          }
        ],
        typeId: [
          { required: true, message: "请选择合作分类", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getdirectoryList();
    this.getmiceList(0);
    this.getmerchantList();
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight =
        window.innerHeight - this.$refs.searchHead.offsetHeight - 220;
    });
  },
  methods: {
    getmiceList(type) {
      miceList({ page: 0, pageSize: 999999 ,type}).then(res => {
        if (res.status) {
          this.mices = res.data.list.map(e => {
            return {
              value: e.miceId,
              label: e.miceName
            };
          });
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    getdirectoryList() {
      directoryList({ page: 0, pageSize: 999999 }).then(res => {
        if (res.status) {
          this.directorys = res.data.list.map(e => {
            return {
              label: e.name,
              value: e.id
            };
          });
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    // 上传文件
    uploadImg(imgs, k, multiple) {
      if (multiple) {
        this.formdata[k] = JSON.parse(imgs);
      } else {
        this.formdata[k] = JSON.parse(imgs)[0];
      }
    },
    getImgUrl(imgs, name, k, multiple) {
      if (multiple) {
        this.formdata[k] = imgs;
      } else {
        this.formdata[k] = imgs[0];
      }
    },
    checkStatus(e, name) {
      this.formdata[name] = e;
    },
    changeStatus(e, name) {
      this.page = 1;
      this.conditions[name] = e;
      this.getmerchantList();
    },
    getmerchantList() {
      //合作列表
      this.loading = true;
      const { page, pageSize } = this;
      const { directoryId, miceId, type } = this.conditions;
      merchantList({
        directoryId,
        miceId,
        type,
        page: page - 1,
        pageSize
      }).then(res => {
        if (res.status) {
          this.total = res.data.count;
          this.list = res.data.list;
          this.loading = false;
        } else {
          this.$Message.error(res.msg);
          this.loading = false;
        }
      });
    },
    show(row) {
      console.log(row);
      this.id = row.id;
      this.formdata = {
        name: row.name,
        phone: row.phone,
        title: row.title,
        intro: row.intro,
        pic: JSON.parse(row.pic)
      };
      this.isShow = true;
    },
    changePage(e) {
      this.page = e;
      this.getmerchantList();
    },
    changeSize(e) {
      this.page = 1;
      this.pageSize = e;
      this.getmerchantList();
    },
    //修改招展询商
    submit(nam) {
      const { name, phone, pic, intro, title } = this.formdata;
      this.$refs[nam].validate(valid => {
        if (valid) {
          modifyMerchant({
            name,
            phone,
            pic: JSON.stringify(pic),
            intro,
            title,
            id: this.id
          }).then(res => {
            if (res.status) {
              this.isShow = false;
              this.getmerchantList();
              this.$Message.success(res.msg);
            } else {
              this.$Message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
    //删除招商询盘信息
    remove(id) {
      this.$Modal.confirm({
        title: "警告",
        content: `删除后无法恢复，确定要删除？`,
        onOk: () => {
          delMerchant({ id }).then(res => {
            if (res.status) {
              this.getmerchantList();
              this.$Message.success("删除成功");
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
        onCancel: () => {}
      });
    },
    cancel() {
      this.isShow = false;
    },
    editorChange(e) {},
    search() {
      this.getmerchantList();
    },
    handleReset() {
      this.conditions = {
        typeId: ""
      };
      this.page = 1;
      this.getmerchantList();
    }
  }
};
</script>
<style lang="less" scoped>
.page {
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}
</style>
